import PerfectScrollbar from 'perfect-scrollbar'
import { addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event'
export default {
  mounted () {
    this.createPs()
    addResizeListener(this.$el, this.updatePs)
  },
  updated () {
    this.updatePs()
  },
  beforeDestroy () {
    this.destroyPs()
    removeResizeListener(this.$el, this.updatePs)
  },
  methods: {
    rebuildPs () {
      this.destroyPs()
      this.createPs()
    },
    updatePs () {
      if (this.ps) {
        // 改为在nextTick之后更新滚动条，例如树节点加载后才更新滚动条（SMX-869）
        this.$nextTick(() => {
          this.ps.update()
        })
      }
    },
    // 销毁滚动条
    destroyPs () {
      if (this.ps) {
        this.ps.destroy()
        this.ps = null
      }
    },
    // 创建滚动条
    createPs () {
      // 滚动条默认开启冒泡模式
      this.ps = new PerfectScrollbar(this.$el, {
        handlers: ['click-rail', 'drag-thumb', 'keyboard', 'wheel', 'touch'],
        wheelSpeed: 1,
        wheelPropagation: true,
        swipeEasing: true,
        minScrollbarLength: null,
        maxScrollbarLength: null,
        scrollingThreshold: 1000,
        useBothWheelAxes: false,
        suppressScrollX: false,
        suppressScrollY: false,
        scrollXMarginOffset: 0,
        scrollYMarginOffset: 0,
      })
    },
  },
}
